package com.qs.springboot.amqp.topic;

import com.qs.springboot.util.ConnectionUtil;
import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * 消费者：绑定exchange，指定订阅routingKey为item.*（通配item下所有一级路径）
 */
public class Recv2 {

    private static final String queue_name = "topic_exchange_queue_2";
    private static final String exchange_name = "topic_exchange_test";

    public static void main(String[] args) throws IOException, TimeoutException {
        Connection connection = ConnectionUtil.getConnection();

        Channel channel = connection.createChannel();

        channel.queueDeclare(queue_name, false, false, false, null);

        //将该消费端对应的队列绑定交换机、指定订阅的routingKey（注意这里的*，匹配item下面的所有自己路径）
        channel.queueBind(queue_name, exchange_name, "item.*");

        DefaultConsumer consumer = new DefaultConsumer(channel) {

            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                String message = new String(body);
                System.out.println("consumer2-received：" + message);
            }
        };

        //监听队列，自动ack
        channel.basicConsume(queue_name, true, consumer);
    }
}
